package cn.monkey.data.mongo;

import cn.monkey.data.entity.dto.TenantQueryRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface MongoTenantEntityRepository<Q extends TenantQueryRequest, T, ID> extends MongoQueryRequestRepository<Q, T, ID> {

    @Override
    default Query buildQuery(TenantQueryRequest tenantQueryRequest) {
        Query query = new Query();
        MongoRepositoryBehaviorContext<T> mongoRepositoryContext = this.getMongoRepositoryContext();
        if (mongoRepositoryContext == null) {
            return query;
        }
        mongoRepositoryContext.beforeSelectBehavior().ifPresent(behavior -> {
            Criteria criteria = behavior.beforeSelect(tenantQueryRequest);
            if (criteria != null) {
                query.addCriteria(criteria);
            }
        });
        return query;
    }
}
